package com.xuanwei.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuanwei.core.cache.CategoryCache;
import com.xuanwei.core.exception.XwException;
import com.xuanwei.core.service.IXwCategoryService;
import com.xuanwei.dao.entity.XwCategory;
import com.xuanwei.dao.mapper.XwCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2019-04-14
 */
@Service
public class XwCategoryServiceImpl extends ServiceImpl<XwCategoryMapper, XwCategory> implements IXwCategoryService {

    @Autowired
    private CategoryCache categoryCache;

    @Override
    public void add(String name, String subName, String pic, Integer uid, Integer shortOrder, Integer show) {
        if (findByName(name) != null) {
            throw new XwException("目录名已存在");
        }
        XwCategory category = new XwCategory();
        category.setName(name);
        category.setSubName(subName);
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateTime(LocalDateTime.now());
        category.setHandleId(uid);
        category.setShortOrder(shortOrder);
        category.setImageUrl(pic);
        category.setParentId(0);
        category.setShowX(show);
        save(category);
        categoryCache.update();

    }


    @Override
    public void delete(Integer id) {
        List<XwCategory> xwCategories = findByParentId(id);
        if (xwCategories != null && xwCategories.size() > 0) {
            throw new XwException("该目录有子目录不能删除");
        }
        getBaseMapper().deleteById(id);
        categoryCache.update();
    }

    private XwCategory findByName(String name) {
        QueryWrapper<XwCategory> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        wrapper.eq("name", name);
        return getOne(wrapper);
    }

    private List<XwCategory> findByParentId(Integer id) {
        QueryWrapper<XwCategory> wrapper = new QueryWrapper<>();
        wrapper.select("id,`name`,image_url");
        wrapper.eq("parent_id", id);
        return getBaseMapper().selectList(wrapper);
    }

    private XwCategory findById(Integer id) {
        QueryWrapper<XwCategory> wrapper = new QueryWrapper<>();
        wrapper.select("id,parent_id");
        wrapper.eq("id", id);
        return getBaseMapper().selectOne(wrapper);
    }

    @Override
    public void update(Integer id, String name, String subName, String pic, Integer uid, Integer shortOrder, Integer show) {

        XwCategory category = findById(id);
        if (category == null) {
            throw new XwException("该数据不存在");
        }
        XwCategory byName = findByName(name);
        if (byName != null && byName.getId() != id) {
            throw new XwException("目录名已存在");
        }
        category.setName(name);
        category.setSubName(subName);
        category.setUpdateTime(LocalDateTime.now());
        category.setHandleId(uid);
        category.setShortOrder(shortOrder);
        category.setImageUrl(pic);
        category.setShowX(show);
        getBaseMapper().updateById(category);
        categoryCache.update();
    }

    @Override
    public List<XwCategory> all() {
        QueryWrapper<XwCategory> wrapper = new QueryWrapper<>();
        wrapper.select("id,parent_id,`name`,sub_name,image_url,short_order,show_x");
        wrapper.orderByDesc("short_order");
        return list(wrapper);
    }


}
